package com.qiyuesuo.file.controller;

import com.qiyuesuo.file.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * @param file
     * @return java.util.Map<java.lang.String, java.lang.String>
     */
    @PostMapping("/upload")
    public Map<String, String> uploadFile(MultipartFile file) {
        if (file == null) {
            throw new RuntimeException("上传文件为空");
        }
        Map<String, String> map;
        map = fileService.uploadFile(file);

        return map;
    }


    /**
     * @param newName
     * @param rs
     * @return void
     */
    @GetMapping("download/{uuid}")
    public void downloadFile(@PathVariable("uuid") String newName, HttpServletResponse rs) {
        fileService.downloadFile(newName, rs);

    }

    /**
     * 根据uuid获取信息
     *
     * @param newName
     * @return java.util.Map
     */
    @GetMapping("get/{uuid}")
    public Map getFileData(@PathVariable("uuid") String newName) {
        Map map = fileService.getFileData(newName);
        return map;
    }
}
